package com.hsmpay.utils;

import java.util.*;

public class YL_ICTLV {
	//分解IC卡数据(TLV(标签,长度,数据)格式)
	public static Map<String,String> splicICBankCardDate(String bit55){
		Map<String,String> result = new HashMap<String,String>();
	    int start = 0;
	    int sumLen = bit55.length();
	    int pshort = 0;
	    //String result = "";
	    int i = 1;
	    while (start < sumLen) {
	    	//System.out.print("i = "+i + " start = "+start);
	        String tag = bit55.substring(start, start+2);
	        pshort = Integer.parseInt(tag,16);
	        if (!"8F".equals(tag) && !"6F".equals(tag) && !"4F".equals(tag) && ((pshort & 0x01) == 1) && ((pshort & 0x02) >> 1 == 1) && ((pshort & 0x04) >> 2 == 1) && ((pshort & 0x08) >> 3 == 1)) {
	            tag = bit55.substring(start, start+4);
	            start += 4;
	        }
	        else {
	            start += 2;
	        }
	       // System.out.print(" tag = "+tag);
//	        if("9F41".equals(tag)){
//	        	System.out.print(" tag = "+tag);
//	        }
	        
	        String len = bit55.substring(start, start+2);
	       // System.out.print(" len = "+len);
	        start += 2;
	        pshort = Integer.parseInt(tag,16);
//	        if ((pshort & 0x80) >> 7 == 1) {
//	            len = bit55.substring (start, start+(2 * (pshort - 128)));
//	            start += 2 * (pshort - 128);
//	        }
	       // System.out.print(" valsEnd = "+start +" "+(start+(Integer.parseInt(len,16) * 2)));
	        String val = bit55.substring(start, start+(Integer.parseInt(len,16) * 2));
	      //  System.out.print(" val = "+val);
	        start += Integer.parseInt(len,16) * 2;
	        //System.out.println(" end = "+start);
	        //result += tag + ":" + val + "&";
	        result.put(tag, len+val);
	        System.out.println(tag+":"+len+val); 
	        i++;
	    }
	    //result = result.substring(0, result.length() - 1);
	    return result;
	}
	
	public static void main(String args[]){
		//System.out.println(Integer.parseInt("A0", 16));
		Map<String,String> map = splicICBankCardDate("9F260868459BD2E8ED6CA89F2701809F101307010103A08002010A010000000000A6D6F1849F3704DEADBAD69F360200399505000004E8009A031504159C01009F02060000000000005F2A02015682027C009F1A0201569F03060000000000009F3303E0E1C09F34030203009F3501219F1E0830303033343932318408A0000003330101019F090200209F410400000261");
		System.out.println("");
		//map key 排序
		Set<String> keySet = map.keySet();  
		List<String> list = new ArrayList<String>();
		list.addAll(keySet);
		Collections.sort(list, new Comparator<String>(){
			public int compare(String c1, String c2) {
				int code1 = Integer.parseInt(c1,16);
				int code2 = Integer.parseInt(c2,16);
				if(code1 < code2){
					return -1 ;
				}else if(code1 > code2){
					return 1 ;
				}else{
					return 0;
				}
			}
		});
		for(int i=0 ;i<list.size();i++) {  
		    String key = list.get(i);  
		    System.out.println(key+":"+map.get(key));  
		}  
	}

}
